'use strict';

const Controller = require('egg').Controller;

class SideController extends Controller {
  async getPosture() {
    const sqlStr = 'SELECT * FROM person_postures_side';
    const result = await this.ctx.app.model.query(sqlStr, { type: 'SELECT' });
    const { postures, errorCodes } = getPoseArrAndErrorCode(result);
    this.ctx.body = { code: 0, message: "success", data: { postures, errorCodes } };
  }
  async addPosture() {
    // const { create_time, top, neck, p_id, ankle_r, knee_r, hip_r, ankle_l, knee_l, hip_l, wrist_l, elbow_l, shoulder_l, wrist_r, elbow_r, shoulder_r } = this.ctx.request.body;
    const requestBody = this.ctx.request.body;
    const { nameStr, valueStr } = getNameAndValue(requestBody);
    if (nameStr === '' || valueStr === '') {
      this.ctx.body = { code: -1, message: "传参为空" };
    } else {
      // const sqlStr2 = "INSERT INTO postures (`create_time`, `top`) VALUES ('2019-02-16', '(2,3)')";
      const sqlStr2 = `INSERT INTO person_postures_side (${nameStr.substr(0, nameStr.length - 1)}) VALUES (${valueStr.substr(0, valueStr.length - 1)})`;
      await this.ctx.app.model.query(sqlStr2, { type: 'INSERT' });
      this.ctx.body = { code: 0, message: "success" };
    }
  }
}

module.exports = SideController;